from django.db import models
from wanbooks.catalog.models import Edition, add_edition
from django.contrib.auth.models import User

class Item(models.Model):
    u'''An examplar'''
    edition = models.ForeignKey(Edition)
    owner   = models.ForeignKey(User)
    
    def __unicode__(self):
        return u'''%s's "%s"''' % (self.owner.username, self.edition.title)

def add_items(items, user):
    new_editions = new_creators = 0
    for edition in items:
        edition, added_editions, added_creators = add_edition(edition)
        Item.objects.create(edition = edition, owner = user)
        new_editions += added_editions
        new_creators += added_creators
    return (new_editions, new_creators)

class Lease(models.Model):
    u'''A lease transaction'''
    item = models.ForeignKey(Item)
    borrower = models.ForeignKey(User)
    duration_requested = models.PositiveIntegerField(null=True)
    duration_granted = models.PositiveIntegerField(null=True)
    scheduled_delivery_dt = models.DateTimeField(null=True)
    actual_delivery_dt = models.DateTimeField(null=True)
    actual_return_dt = models.DateTimeField(null=True)
    request_dt = models.DateTimeField(auto_now_add=True)
    
    def __unicode__(self):
        return u'''%s's "%s" -> %s''' % (self.owner.username,
                                   self.edition.title, self.borrower.username)
        